//
// Created by wsl on 8/12/22.
//
#include "AffineCircle.h"

cv::Mat AffineCircle::getMatrix(std::vector<cv::Mat> &img0, std::vector<cv::Mat> &img1) {
    assert(img0.size()==img1.size());
    cv::Point2f src_points[3];
    cv::Point2f dst_points[3];

    for(int i=0;i<img0.size();i++)
    {
        auto circle0= getCircle(img0[i],true);
        auto circle1= getCircle(img1[i], false);

        dst_points[i]=cv::Point2f(circle0[0][0],circle0[0][1]);
        src_points[i]=cv::Point2f(circle1[0][0],circle1[0][1]);
    }
    cv::Mat matrix=cv::getAffineTransform(src_points,dst_points);
    return matrix;
}

std::vector<cv::Vec3f> AffineCircle::getCircle(cv::Mat img, bool flag0) {
    cv::cvtColor(img,img,cv::COLOR_BGR2GRAY);
    cv::medianBlur(img,img,3);
    cv::GaussianBlur(img,img,cv::Size(17, 19),0);
    std::vector<cv::Vec3f> circles;
    if(flag0)
    {
        cv::HoughCircles(img,circles,cv::HOUGH_GRADIENT,1,20,50,30,minr0,maxr0);
    } else{
        cv::HoughCircles(img,circles,cv::HOUGH_GRADIENT,1,20,50,30,minr1,maxr1);
    }
    return circles;
}

cv::Point2f AffineCircle::warpPoint(const cv::Point2f& src, cv::Mat matrix) {
    cv::Mat src_array=cv::Mat::ones(cv::Size(3,1),CV_16F);
    src_array.data[0]=src.x;
    src_array.data[1]=src.y;
    src_array=matrix*src_array;
    cv::Point2f results(src_array.data[0],src_array.data[1]);
    return results;
}
